package de.clapp.newsfeed;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.clapp.IModelAdapter;
import de.clapp.R;
import de.clapp.data.Newsfeed;
import de.clapp.toolkit.ImageManager;

public class NewsfeedAdapter extends ArrayAdapter<Newsfeed> implements IModelAdapter
{
    private ArrayList<Newsfeed> values;

    private LayoutInflater inflater;

    private NewsfeedFragment newsfeedFragment;

    public NewsfeedAdapter(NewsfeedFragment newsfeedFragment)
    {
        super(newsfeedFragment.getActivity(), R.layout.newsfeed_entry);
        this.newsfeedFragment = newsfeedFragment;
        this.inflater = (LayoutInflater) newsfeedFragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.values = new ArrayList<Newsfeed>();
    }

    @Override
    public int getCount()
    {
        return values.size();
    }

    @SuppressWarnings("unchecked")
    public <D> void setData(D values)
    {
        this.values = (ArrayList<Newsfeed>) values;
        clear();
        addAll(this.values);
        newsfeedFragment.setListShown(true);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.newsfeed_entry, parent, false);

            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.newsfeed_entry_title);
            holder.content = (TextView) convertView.findViewById(R.id.newsfeed_entry_content);
            holder.image = (ImageView) convertView.findViewById(R.id.newsfeed_entry_image);

            convertView.setTag(holder);

        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        // Inhalt aus der Liste eintragen
        holder.title.setText(values.get(position).getTitle());
        holder.content.setText(values.get(position).getContent());
        holder.image.setTag("url");
        // ImageManager.getInstance().loadBitmap("url", holder.image, 32, 32);

        return convertView;
    }

    /**
     * The Class ViewHolder.
     */
    private static class ViewHolder
    {
        /** The logo image view. */
        public ImageView image;

        /** The title text view. */
        public TextView title;

        /** The content text view. */
        public TextView content;
    }

}
